var ns,app;
app = new Vue({
    el: '#app',
    data: {
        showsearch: false,
        showsearchmonth: false,
        showsearchparam: false,
        roomlist: [],
        form: {
            startTime: '',
            endTime: '',
            activeDate: 0,
            ProjectID:0,
            ProjectIDs: [],
            playtypeID:0,
            playtypeListIDS:[],//作业类型
            overtimeID:0,
            overtimeListIDS:[],//超时状态
            headListIDS:[],//负责人
            statusListIDS: [],//状态
            activeDateDesc: '时间',
            TaskType:1,//1 计划任务 2临时记录
        },
        ProjectName:'',
        projectList: [],
        CheckChart:{},
        RepairChart:{},
        summary: {
            Count: 0,
            ValidCount: 0,
            NoValidCount: 0,
        },
        tempsummary: {
            Count: 0,
            CheckCount: 0,
            RepairCount: 0,
        },
        lssummary: {
            Count: 0,
            NormalCount: 0,
            NoNormalCount: 0,
        },
        IsAnalysis:true,
        playtypeList:[
            { "ID": "0", "Name": "全部" },
            { "ID": "1", "Name": "巡检任务" },
            { "ID": "2", "Name": "维保任务" },
        ],
        overtimeList:[
            { "ID": "0", "Name": "全部" },
            { "ID": "1", "Name": "未超时" },
            { "ID": "2", "Name": "已超时" },
        ],
        headList:[
            { "ID": "0", "Name": "全部" },
            // { "ID": "34", "Name": "唐娟" },
            // { "ID": "17", "Name": "刘关键" },
        ],
        statusList: [
            { "ID": "0", "Name": "全部" },
            { "ID": "1", "Name": "未开始" },
            { "ID": "2", "Name": "执行中" },
            { "ID": "3", "Name": "已完成" },
            { "ID": "4", "Name": "已结束" },
        ],
        userList:[],
    },
    methods: {
        get_data: function () {
            var that = this;
            that.getSearchParams();
            setTimeout(function () {
                that.getAnalysisData();
            }, 1000)
        },
        doSelectPlayType: function (ID) {
            var that = this;
            that.form.playtypeID=ID;
            that.form.playtypeListIDS=[];
            that.form.playtypeListIDS.push(ID);
        },
        doSelectOverTime: function (ID) {
            var that = this;
            that.form.overtimeID=ID;
            that.form.overtimeListIDS=[];
            that.form.overtimeListIDS.push(ID);
        },
        doSelectHead: function (ID) {
            var that = this;
            var index = that.form.headListIDS.indexOf(ID);
            if (index > -1) {
                that.form.headListIDS.splice(index, 1);
            } else {
                that.form.headListIDS.push(ID);
            }
        },
        doSelectstatus: function (ID) {
            var that = this;
            var index = that.form.statusListIDS.indexOf(ID);
            if (index > -1) {
                that.form.statusListIDS.splice(index, 1);
            } else {
                that.form.statusListIDS.push(ID);
            }
        },
        doSelectProject: function (ID,Name) {
            var that = this;
            // that.ProjectName=Name;
            // var index = that.form.ProjectIDs.indexOf(ID);
            // if (index > -1) {
            //     that.form.ProjectIDs.splice(index, 1);
            // } else {
            //     that.form.ProjectIDs.push(ID);
            // }
            that.ProjectName=Name;
            that.form.ProjectID=ID;
            that.form.ProjectIDs=[];
            that.form.ProjectIDs.push(ID);
            // that.showsearchparam = false;
            // that.doSearch();
        },
        getSearchParams: function () {
            var that = this;
            var options = {};
            options.action = "getanalysissearchparam";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.projectList = data.projectList;
                    if(that.projectList!=undefined&&that.projectList.length>0){
                        that.doSelectProject(that.projectList[0].ID,that.projectList[0].Name); 
                    }
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        getAnalysisData: function () {
            var that = this;
            //处理状态
            var options = {};
            options.action = "gettaskanalysisdata";
            options.ProjectID = that.form.ProjectID;
            options.QueryType = that.form.TaskType;
            options.PlanType = that.form.playtypeID;
            options.ValidStatus = that.form.overtimeID;
            options.TaskStatuss = JSON.stringify(that.form.statusListIDS);
            options.StartTime = that.form.startTime;
            options.EndTime = that.form.endTime;
            // console.log(JSON.stringify(options));
            ns.post(options, function (succeed, data, err) {
                // console.log(JSON.stringify(data));
                if (succeed) {
                    that.CheckChart={};
                    that.RepairChart={};
                    if(that.form.TaskType==1){
                        that.summary=data.data.TaskData;
                        that.CheckChart=data.data.CheckData;
                        that.RepairChart=data.data.RepairData;
                        setTimeout(function(){
                            that.loadChart(that.CheckChart,'xjcontainer');
                        },100);  
                        setTimeout(function(){
                            that.loadChart(that.RepairChart,'wbcontainer');
                        },200);  
                    }
                    if(that.form.TaskType==2){
                        that.tempsummary=data.data.TempData;
                        that.lssummary=data.data.CompleteData;
                    }
                    that.userList=data.data.UserData;           
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        doReset: function (type) {
            var that = this;
            if (type == 2) {
                that.form.startTime = '';
                that.form.endTime = '';
                that.form.activeDate = 0;
                that.form.activeDateDesc = '时间';

            } else {
                that.form.ProjectIDs = [];
                that.form.statusListIDS = [];
            }
        },
        doSearch: function () {
            var that = this;
            that.doHideSearch();
            that.getAnalysisData();
        },
        doSelectSearchParam: function (type) {
            var that = this;
            if (type == 1) { //筛选
                that.showsearchparam = !that.showsearchparam;
                that.showsearchmonth = false;
            }
            if (type == 2) { //按月
                that.showsearchparam = false;
                that.showsearchmonth = !that.showsearchmonth;
            }
            that.showsearch = that.showsearchparam || that.showsearchmonth;
            if (type == 2 && that.showsearchmonth) {
                setTimeout(function () {
                    that.initPicker();
                }, 100);
            }
        },
        doHideSearch: function () {
            var that = this;
            that.showsearchparam = false;
            that.showsearchmonth = false;
            that.showsearch = false;
        },
        doClickDay: function (dataValue) {
            var that = this;
            that.form.activeDate = dataValue;
            var startDate = '',
                endDate = '';
            if (dataValue == 1) {
                var currentDate = dateRangeUtil.getCurrentDate();
                startDate= moment(new Date()).subtract(3, 'days').format("YYYY-MM-DD");
                endDate =  dateRangeUtil.getDateString(currentDate);
                that.form.activeDateDesc = '近三天';
            }
            if (dataValue == 2) {
                var currentDate = dateRangeUtil.getCurrentDate();
                startDate= moment(new Date()).subtract(7, 'days').format("YYYY-MM-DD");
                endDate= dateRangeUtil.getDateString(currentDate);
                that.form.activeDateDesc = '近七天';
            }
            if (dataValue == 3) {
                var currentDate = dateRangeUtil.getCurrentDate();
                startDate= moment(new Date()).subtract(30, 'days').format("YYYY-MM-DD");
                endDate= dateRangeUtil.getDateString(currentDate);
                that.form.activeDateDesc = '近三十天';
            }
            that.form.startTime = startDate;
            that.form.endTime = endDate;
        },
        loadChart: function (form,id) {
            var chart = Highcharts.chart(id, {
                chart: {
                    spacing: [20, 0, 20, 0]
                },
                credits: {
                    enabled: false //不显示LOGO
                },
                navigation: {
                    buttonOptions: {
                        enabled: false
                    }
                },
                title: {
                    floating: true,
                    text: '任务总数',
                    style: { "color": "#999", "fontSize": "13px" },
                },
                subtitle: {
                    floating: true,
                    text: form.Count,
                    style: { "color": "#333", "fontSize": "14px" },
                    verticalAlign: 'middle',
                    y: 15,
                },
                plotOptions: {
                    pie: {
                        size: '100%',
                        allowPointSelect: false,
                        cursor: 'pointer',
                        dataLabels: {
                            enabled: true,
                            format: '{point.name}: {y} ',
                            style: {
                                color:"#666",
                            }
                        },
                        showInLegend: true,
                    }
                },
                series: [{
                    type: 'pie',
                    innerSize: '80%',
                    name: '',
                    data: [
                        ['未开始',form.NoCompleteCount],
                        ['执行中', form.ProgressCount],
                        ['已完成', form.CompleteCount],
                        ['已结束', form.EndCount]
                    ]
                }]
            }, function (c) { // 图表初始化完毕后的会掉函数
                // 环形图圆心
                var centerY = c.series[0].center[1],
                    titleHeight = parseInt(c.title.styles.fontSize);
                // 动态设置标题位置
                c.setTitle({
                    y: centerY + titleHeight / 2-10
                });
            });
        
        },
        do_select_time: function (type) {
            var that = this;
            var currentTime = type == 1 ? that.form.startTime : that.form.endTime;
            api.openPicker({
                type: 'date',
                date: currentTime,
                title: '选择日期'
            }, function (ret, err) {
                if (ret) {
                    var year = ret.year;
                    var month = (ret.month >= 10 ? ret.month : '0' + ret.month);
                    var day = (ret.day >= 10 ? ret.day : '0' + ret.day);
                    currentTime = year + '-' + month + '-' + day;
                    if (type == 1) {
                        that.form.startTime = currentTime;
                    } else {
                        that.form.endTime = currentTime;
                    }
                }
            });
        },
        do_open_gongdan: function (item, type) {
            var that = this;
            ns.openWin('home_task_con_frm', '任务详情', {
                id: item.ID,
                type: type,
                isviewdetail: that.form.isviewdetail
            });
        },
        initPicker: function () {
            var _this = this;
            (function ($, doc) {
                $.init();
                $.ready(function () {
                    //开始日期
                    var btn = document.querySelector('#startdate');
                    btn.addEventListener('tap', function () {
                        var _self = this;
                        if (_self.picker) {
                            if(_this.form.startTime){
                                _self.picker.setSelectedValue(_this.form.startTime);//回显
                            }
                            _self.picker.show(function (rs) {
                                _this.form.startTime = rs.value;
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        } else {
                            var optionsJson = this.getAttribute('data-options') || '{}';
                            var options = JSON.parse(optionsJson);
                            var id = this.getAttribute('id');
                            _self.picker = new $.DtPicker(options);
                            if(_this.form.startTime){
                                _self.picker.setSelectedValue(_this.form.startTime);//回显
                            }
                            _self.picker.show(function (rs) {
                                var timer = setTimeout(function () {
                                    clearTimeout(timer);
                                    _this.form.startTime = rs.value;
                                }, 300);
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        }
                    }, false);
                    //结束日期
                    var btnEnd = document.querySelector('#enddate');
                    btnEnd.addEventListener('tap', function () {
                        var _self = this;
                        if (_self.picker) {
                            if(_this.form.endTime){
                                _self.picker.setSelectedValue(_this.form.endTime);//回显
                            }
                            _self.picker.show(function (rs) {
                                _this.form.endTime = rs.value;
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        } else {
                            var optionsJson = this.getAttribute('data-options') || '{}';
                            var options = JSON.parse(optionsJson);
                            var id = this.getAttribute('id');
                            _self.picker = new $.DtPicker(options);
                            if(_this.form.endTime){
                                _self.picker.setSelectedValue(_this.form.endTime);//回显
                            }
                            _self.picker.show(function (rs) {
                                var timer = setTimeout(function () {
                                    clearTimeout(timer);
                                    _this.form.endTime = rs.value;
                                }, 300);
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        }
                    }, false);
                });
            })(mui, document);
        },
        doSelectTaskType:function(val){
            this.form.TaskType=val;
            this.getAnalysisData();
        },
        open_task_detail:function(){
            var that = this;
            var title='设备巡检任务列表';
            var url='equipment_tasklist';
            ns.openWin(url, title, {
                ProjectID: that.form.ProjectID,
                ProjectName:that.ProjectName,
                startMonth:that.form.startMonth,
                IsAnalysis:that.IsAnalysis,
            });
        },
        open_task_result:function(){
            var that = this;
            var url = 'equipment_tasklist_record_frm';
            ns.openWin(url, "设备作业结果", {
                IsAnalysis:that.IsAnalysis,
                ProjectID:that.form.ProjectID,
                QueryType:that.form.TaskType,
                PlanType:that.form.playtypeID,
                ValidStatus:that.form.overtimeID,
                TaskStatus:JSON.stringify(that.form.statusListIDS),
                StartTime:that.form.startTime,
                EndTime:that.form.endTime, 
            });
        },
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    app.doClickDay(3);
    app.doSelectPlayType(app.playtypeList[0].ID);
    app.doSelectOverTime(app.overtimeList[0].ID);
    app.doSelectHead(app.headList[0].ID);
    app.doSelectstatus(app.statusList[0].ID);

    setTimeout(function () {
        app.get_data();
    }, 500)
};
